//
//  TTRecyclingUploadGoodsViewController.m
//  TaoTuo
//
//  Created by apple on 2020/9/17.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTRecyclingUploadGoodsViewController.h"
#import "TTRecyclingUploadGoodsHeaderView.h"
#import "TTRecyclingCommonViewCell.h"

static NSString *cellID = @"cellID";

@interface TTRecyclingUploadGoodsViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) TTRecyclingUploadGoodsHeaderView *headerView;


@end

@implementation TTRecyclingUploadGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupDatas];
    
    @weakify(self)
    [self.headerView.refreshSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        [self.tableView reloadData];
    }];
    
}

- (void)setupDatas
{
    [self.dataArray removeAllObjects];
    
    TTRecyclingCellModel *model1 = [TTRecyclingCellModel setupModelWithTitle:@"销售模式" content:@"" placeholder:@"请选择销售模式" rightString:@"" cellType:arrowTpey target:self action:@selector(chooseSellType)];
    
    TTRecyclingCellModel *model2 = [TTRecyclingCellModel setupModelWithTitle:@"价格" content:@"" placeholder:@"输入价格只能为整数" rightString:@"元" cellType:labelType keyboardType:UIKeyboardTypeNumberPad target:nil action:nil];
    
    [self.dataArray addObject:model1];
    [self.dataArray addObject:model2];
    
    
    self.tableView.tableHeaderView = self.headerView;
    
    [self.tableView reloadData];
}

/// 选择销售模式
- (void)chooseSellType
{
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTRecyclingCommonViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTRecyclingCellModel *model = self.dataArray[indexPath.row];
    if (model.target && model.action) {
        if ([model.target respondsToSelector:model.action]) {
            [model.target performSelector:model.action];
        }
    }
}

- (TTRecyclingUploadGoodsHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [[TTRecyclingUploadGoodsHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    }
    return _headerView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 50;
        [_tableView registerClass:[TTRecyclingCommonViewCell class] forCellReuseIdentifier:cellID];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_topLayoutGuideBottom);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideTop).mas_offset(-50);
        }];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
